package com.nccsjz.front.service;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletContext;

import com.nccsjz.back.bulletin.service.BulletinService;
import com.nccsjz.back.cardway.service.CardWayService;
import com.nccsjz.back.customer.service.CustomerService;
import com.nccsjz.back.faq.service.FaqService;
import com.nccsjz.back.merch.service.MerchService;
import com.nccsjz.back.news.service.NewsService;
import com.nccsjz.base.BaseService;
import com.nccsjz.front.dao.IndexCardDAO;
import com.nccsjz.pojo.BatExportFhtb;
import com.nccsjz.pojo.Bulletin;
import com.nccsjz.pojo.CardWay;
import com.nccsjz.pojo.Faq;
import com.nccsjz.pojo.Merchant;
import com.nccsjz.pojo.News;
import com.nccsjz.pojo.Vote;

public class IndexCardService extends BaseService {

	/**
	 * 得到最新公告
	 * 
	 * @return
	 */
	public Bulletin getLastBulletin() {
		Bulletin bulletin = null;
		try {
			bulletin = new BulletinService().getLastBulletin();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (null == bulletin) {
			bulletin = new Bulletin();
			bulletin.setBulletincontent("暂时还没有公告");
		}
		return bulletin;
	}

	/**
	 * 得到前n条新闻
	 * 
	 * @param n
	 * @return
	 */
	public List<News> getTopNews(int n) {
		return new NewsService().getTopN(n);
	}

	/**
	 * 得到前N条常见问题。
	 * 
	 * @param topN
	 * @return
	 */
	public List<Faq> getTopFaqs(int topN) {
		return new FaqService().getTopN(topN);
	}

	/**
	 * 得到前N条用卡知识。
	 * 
	 * @param topN
	 * @return
	 */
	public List<CardWay> getTopCardWays(int topN) {

		return new CardWayService().getTopN(topN);
	}

	/**
	 * 得到前N条用卡知识。
	 * 
	 * @param topN
	 * @return
	 */
	public List<Merchant> getTopCardMerchs(int topN) {

		return new MerchService().getTopN(topN);
	}

	/**
	 * 得到当前投票主题
	 * 
	 * @return
	 * @throws Exception
	 */
	public Vote getSelectedVote() throws Exception {
		this.openCon();
		Vote vote = new IndexCardDAO(con).getSelectedVote();
		this.closeCon();
		return vote;
	}

	/**
	 * 得到产品展示图片列表
	 * 
	 * @param path
	 * @return
	 */
	public List getProPics(ServletContext context) {
		String path = context.getRealPath("") + File.separator + "userfiles"
				+ File.separator + "image";		
		return new IndexCardDAO(con).getProPic(path);
	}

	/**
	 * 验证登录会员。
	 * 
	 * @param mname
	 * @param mpwd
	 * @return 是卡户返回1，是商户返回2。
	 * @throws Exception
	 */
	public int checkUser(String mname, String mpwd) throws Exception {
		int stat = 0;
		this.openCon();
		stat = new IndexCardDAO(con).checkUser(mname, mpwd);
		this.closeCon();
		return stat;
	}
	
	/**
	 * isMerchantPass方法用于检查商户已经审核通过
	 * 
	 * @param mname
	 * @param mpwsd
	 * @return
	 * @throws SQLException
	 * @throws IOException 
	 */
	public boolean isMerchantPass(String mname,String mpwsd) throws SQLException, IOException{
		boolean flag = false;
		try{
			this.openCon();
			Merchant m = new MerchService().getMerchByNameAndPwd(mname, mpwsd);
			if(m != null && m.getMstat() == 1L){
				flag = true;
			}
		}finally{
			this.closeCon();
		}		
		return flag;		
	}

	/**
	 * 查询消费记录
	 * 
	 * @param startDate
	 * @param endDate
	 * @param acc
	 * @return
	 * @throws SQLException
	 */
	public List<BatExportFhtb> buySomething(String startDate, String endDate,
			String acc, String type) throws Exception {
		this.openCon();
		List<BatExportFhtb> bats = new IndexCardDAO(con).buySomething(
				startDate, endDate, acc, type);
		this.closeCon();
		return bats;
	}

	/**
	 * 查询记录
	 * 
	 * @param startDate
	 * @param endDate
	 * @param acc
	 * @return
	 * @throws SQLException 
	 * @throws SQLException
	 */
	public List<BatExportFhtb> payoffSomething(String startDate,
			String endDate, String acc, String jIAOFEI) throws SQLException {
		this.openCon();
		List<BatExportFhtb> bats = new IndexCardDAO(con).payoffSomething(
				startDate, endDate, acc, jIAOFEI);
		this.closeCon();
		return bats;
	}
/**
 * 得到注册用户ID
 * @param mname
 * @param mpwd
 * @return
 * @throws SQLException
 * @throws IOException 
 */
	public long getUserID(String mname, String mpwd) throws SQLException, IOException {
		long userid=-1;
		//卡户
		if(null!=new CustomerService().getCusByNameAndPwd(mname, mpwd)){
			userid=new CustomerService().getCusByNameAndPwd(mname, mpwd).getMid();
		}
		Merchant mer=new MerchService().getMerchByNameAndPwd(mname, mpwd);
		//商户
		if(null!=mer){
			userid=mer.getMid();
		}
		
		return userid;
	}

}
